home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Leser 15 / Amiga Plus Leser CD 15.iso / Tools / Development / MosaicSRC / libwww2 / HTAtom.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-13  |  2.6 KB  |  112 lines

  1. /*            Atoms: Names to numbers            HTAtom.c
  2. **            =======================
  3. **
  4. **    Atoms are names which are given representative pointer values
  5. **    so that they can be stored more efficiently, and comparisons
  6. **    for equality done more efficiently.
  7. **
  8. **    Atoms are kept in a hash table consisting of an array of linked lists.
  9. **
  10. ** Authors:
  11. **    TBL    Tim Berners-Lee, WorldWideWeb project, CERN
  12. **    (c) Copyright CERN 1991 - See Copyright.html
  13. **
  14. */
  15. #define HASH_SIZE    101        /* Tunable */
  16. #include "HTAtom.h"
  17.  
  18. #include <stdio.h>                /* joe@athena, TBL 921019 */
  19. #include "HTUtils.h"
  20. #ifdef _AMIGA
  21.    #include <string.h>                           /* MJW 931003 */
  22. #include "tcp.h"
  23.  
  24. PRIVATE HTAtom * hash_table[HASH_SIZE];
  25. PRIVATE BOOL initialised = NO;
  26.  
  27. #ifdef __STDC__
  28. PUBLIC HTAtom * HTAtom_for(char * string)
  29. #else
  30. PUBLIC HTAtom * HTAtom_for(string)
  31.     char * string;
  32. #endif
  33. {
  34.     int hash;
  35.     CONST char * p;
  36.     HTAtom * a;
  37.  
  38.     /* Bug hack. */
  39.     if (!string || !*string)
  40.       string = strdup ("blargh");
  41.     
  42.     /*        First time around, clear hash table
  43.     */
  44.     if (!initialised) {
  45.         int i;
  46.     for (i=0; i<HASH_SIZE; i++)
  47.         hash_table[i] = (HTAtom *) 0;
  48.     initialised = YES;
  49.     }
  50.     
  51.     /*        Generate hash function
  52.     */
  53.     for(p=string, hash=0; *p; p++) {
  54.         hash = (hash * 3 + *p) % HASH_SIZE;
  55.     }
  56.     
  57.     /*        Search for the string in the list
  58.     */
  59.     for (a=hash_table[hash]; a; a=a->next) {
  60.     if (0==strcmp(a->name, string)) {
  61.             /* if (TRACE) fprintf(stderr,
  62.             "HTAtom: Old atom %p for `%s'\n", a, string); */
  63.         return a;                /* Found: return it */
  64.     }
  65.     }
  66.     
  67.     /*        Generate a new entry
  68.     */
  69.     a = (HTAtom *)malloc(sizeof(*a));
  70.     if (a == NULL) outofmem(__FILE__, "HTAtom_for");
  71.     a->name = (char *)malloc(strlen(string)+1);
  72.     if (a->name == NULL) outofmem(__FILE__, "HTAtom_for");
  73.     strcpy(a->name, string);
  74.     a->next = hash_table[hash];        /* Put onto the head of list */
  75.     hash_table[hash] = a;
  76. /*    if (TRACE) fprintf(stderr, "HTAtom: New atom %p for `%s'\n", a, string); */
  77.     return a;
  78. }
  79.  
  80.  
  81. #ifdef __STDC__
  82. PUBLIC HTAtom * HTAtom_exists(char * string)
  83. #else
  84. PUBLIC HTAtom * HTAtom_exists(string)
  85.     char * string;
  86. #endif
  87. {
  88.     int hash;
  89.     CONST char * p;
  90.     HTAtom * a;
  91.     
  92.     if (!initialised) {
  93.       return NULL;
  94.     }
  95.     
  96.     /*        Generate hash function
  97.     */
  98.     for(p=string, hash=0; *p; p++) {
  99.         hash = (hash * 3 + *p) % HASH_SIZE;
  100.     }
  101.     
  102.     /*        Search for the string in the list
  103.     */
  104.     for (a=hash_table[hash]; a; a=a->next) {
  105.     if (0==strcmp(a->name, string)) {
  106.         return a;                /* Found: return it */
  107.     }
  108.     }
  109.     
  110.     return NULL;
  111. }
  112.